package com.tool.mylibrary.network.HttpRequest.manager;


import android.arch.lifecycle.Lifecycle;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okgo.request.PostRequest;
import com.tool.mylibrary.network.HttpRequest.interfaces.IRequest;
import com.tool.mylibrary.network.HttpRequest.interfaces.IRequestCallback;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 文件名: OkGoRequestManager.java
 * 描述：
 * Create by Google on 2018/6/27 18:27
 */

public class OkGoRequestManager implements IRequest {

    private Lifecycle lifecycle;

    public OkGoRequestManager(Lifecycle lifecycle) {
        this.lifecycle = lifecycle;
    }

    public static OkGoRequestManager getInstance(Lifecycle lifecycle) {
        return new OkGoRequestManager(lifecycle);
    }


    @Override
    public void get(String url, IRequestCallback iRequestCallback) {
        doGet(url, null, null, iRequestCallback);

    }

    @Override
    public void get(String url, Map<String, String> params, IRequestCallback iRequestCallback) {
        doGet(url, null, params, iRequestCallback);
    }

    @Override
    public void get(String url, Map<String, String> headers, Map<String, String> params, IRequestCallback iRequestCallback) {
        doGet(url, headers, params, iRequestCallback);
    }

    @Override
    public void post(String url, Map<String, String> params, IRequestCallback iRequestCallback) {
        doPost(url, null, params, null, null, iRequestCallback);
    }

    @Override// 有文件的post  无header
    public void post(String url, Map<String, String> params, String fileKey, List<File> files, IRequestCallback iRequestCallback) {
        doPost(url, null, params, fileKey, files, iRequestCallback);
    }

    @Override
    public void post(String url, Map<String, String> headers, Map<String, String> params, IRequestCallback iRequestCallback) {
        doPost(url, headers, params, null, null, iRequestCallback);
    }

    @Override
    public void post(String url, Map<String, String> headers, Map<String, String> params, String fileKey, List<File> files, IRequestCallback iRequestCallback) {
        doPost(url, headers, params, fileKey, files, iRequestCallback);
    }

    private void doGet(String url, Map<String, String> headers, Map<String, String> params, final IRequestCallback iRequestCallback) {

        final GetRequest<String> request = OkGo.<String>get(url);
        //tag
        request.tag(lifecycle.getClass());
        //header
        if (headers!=null){
            Set<String> keys = headers.keySet();
            if ( keys!=null&&keys.size() > 0) {
                for (String key : keys) {
                    request.headers(key, headers.get(key));
                }
            }
        }


        //请求参
        if (params != null && params.keySet().size() > 0) {
            request.params(params);
        }

        request.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
                    iRequestCallback.onSuccess(response.body());
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
                    iRequestCallback.onFailure(response.getException());
                }

            }
        });
    }


    private void doPost(String url, Map<String, String> headers, Map<String, String> params, String fileKey, List<File> files, final IRequestCallback iRequestCallback) {
        PostRequest<String> request = OkGo.<String>post(url);
        //post 强制使用multipart
        request.isMultipart(true);
        //tag
        request.tag(lifecycle.getClass());
        //header
        //header
        if (headers!=null){
            Set<String> keys = headers.keySet();
            if ( keys!=null&&keys.size() > 0) {
                for (String key : keys) {
                    request.headers(key, headers.get(key));
                }
            }
        }
        //请求参
        if (params != null && params.keySet().size() > 0) {
            request.params(params);
        }
        //文件
        if (files != null && files.size() > 0) {
            request.addFileParams(fileKey, files);
        }

        request.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
                    iRequestCallback.onSuccess(response.body());
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
                    iRequestCallback.onFailure(response.getException());
                }

            }
        });
    }

}
